package com.clerkq.FlvPlayer.ui
{
//	import caurina.transitions.Tweener;
	
	import com.content.model.LoadedAssets.SkinProxy;
	import com.content.view.Component.Button.HighLightButton;
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import gs.TweenLite;
	
	public class VolumeBar extends UIObject implements IDisposable
	{
		public static const SOUND_CHANGE:String = "onDragVolumeBar";
		
		private const WIDTH:Number = 50;
		private const HEIGHT:Number = 10;
		
		private var _glowStick:Sprite;
		private var _slider:HighLightButton;	// 滑条
		private var _volumeRegion:Sprite;
		private var _volumeRegionMask:Sprite;
		private var _hotArea:Sprite;
		
		private var _dragBound:Rectangle;
		
		public function VolumeBar()
		{
			super();
			create();
			addEventListeners();
		}
		
		private function create():void
		{
			// GlowStick
			_glowStick = new SkinProxy.VideoPlayer_VolumeBar() as Sprite;
			_glowStick.alpha = 0;
			addChild(_glowStick);
			
			// PlayedRegion
			_volumeRegion = new SkinProxy.VideoPlayer_VolumeRegion() as Sprite;
			addChild(_volumeRegion);
			_volumeRegionMask = new Sprite();
			_volumeRegionMask.graphics.beginFill(0xffff00, 1.0);
			_volumeRegionMask.graphics.drawRect(0,0,0.1,HEIGHT)
			addChild(_volumeRegionMask);
			_volumeRegion.mask = _volumeRegionMask;
			
			// Only for click, like seek in progressBar
			_hotArea = new SkinProxy.VideoPlayer_VolumeRegion() as Sprite;
			addChild(_hotArea);
			_hotArea.alpha = 0.01;
			
			// Slider Button
			var sliderNormal:Sprite = new SkinProxy.VideoPlayer_Slider() as Sprite;
			var sliderLight:Sprite = new SkinProxy.VideoPlayer_SliderLight() as Sprite;
			_slider = new HighLightButton(sliderNormal, sliderLight);
			_slider.width = 6;
			_slider.height = 12.5;
			_slider.y = 4;
			addChild(_slider);			
			_dragBound = new Rectangle(0,_slider.y,WIDTH ,0);
			
		}
		
		private function addEventListeners():void
		{
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_slider.addEventListener(MouseEvent.MOUSE_DOWN, onSliderMouseDown);
			_slider.addEventListener(MouseEvent.MOUSE_UP, onSliderMouseUp);
			_hotArea.addEventListener(MouseEvent.CLICK, onClickHotArea);
		}
		
		// Events
		private function onMouseOver(evt:MouseEvent):void{
			
//			Tweener.addTween(_glowStick, {alpha:0.7, time:0.6, transition:"easeOutQuint"});		
			TweenLite.to(_glowStick, 0.6, {alpha:0.7});
		}
		
		private function onMouseOut(evt:MouseEvent):void{
			
//			Tweener.addTween(_glowStick, {alpha:0.0, time:0.6, transition:"easeOutQuint"});
			TweenLite.to(_glowStick, 0.6, {alpha:0.0});
		}
		
		private function onSliderMouseDown(evt:MouseEvent):void{
			_slider.startDrag(false, _dragBound);
			stage.addEventListener(MouseEvent.MOUSE_UP, onSliderMouseUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		}
		
		private function onSliderMouseUp(evt:MouseEvent):void{
			_slider.stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_UP, onSliderMouseUp);			
		}
		
		private function onClickHotArea(evt:MouseEvent):void{
			var newPercent:Number = evt.localX/WIDTH;
			percent = newPercent;
			dispatchEvent(new Event(SOUND_CHANGE, true));
		}
		
		private function onMouseMove(evt:MouseEvent):void{
			var sliderX:Number = _slider.x;
			_volumeRegionMask.width = sliderX;
			dispatchEvent(new Event(SOUND_CHANGE, true));
		}
		
		// Get and Set
		public function get percent():Number{
			return _slider.x / WIDTH;
		}
		
		public function set percent(percent:Number):void{
			var sliderX:Number = percent * WIDTH;
			_slider.x = sliderX;
			_volumeRegionMask.width = sliderX;
		}
		
		// Public //////////////////////////
		public function Dispose():void{
//			Tweener.removeTweens(this);
			
			removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_slider.removeEventListener(MouseEvent.MOUSE_DOWN, onSliderMouseDown);
			_slider.removeEventListener(MouseEvent.MOUSE_UP, onSliderMouseUp);
			_hotArea.removeEventListener(MouseEvent.CLICK, onClickHotArea);
			
			stage.removeEventListener(MouseEvent.MOUSE_UP, onSliderMouseUp);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			
			_slider.Dispose();
			
			removeAllChilds();
		}

	}
}